package com.tech.connect.huanzhewan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class HuanZheWanCatChooseActivity extends BaseActivity {

    private RecyclerView mainRecycler;
    private RecyclerView subRecycler;
    private BaseAdapter<String, BaseHolder> mainAdapter;
    private BaseAdapter<String, BaseHolder> subAdapter;
    private List<String> mainList = new ArrayList<>();
    private List<String> subList = new ArrayList<>();
    private int selectPosition = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("选择分类");
        setContentView(R.layout.activity_cat_choose);
        initView();
        loadData();
    }

    private void loadData() {
        initMainAdapter();
        initSubAdapter();
    }

    private void initView() {
        mainRecycler = findViewById(R.id.mainRecycler);
        subRecycler = findViewById(R.id.subRecycler);
        mainRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        mainRecycler.addItemDecoration(new HLineDivider());
        subRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    }

    private void initMainAdapter() {
        if (mainAdapter == null) {
            mainAdapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_cat_choose, mainList, 6) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    TextView tvItem = helper.getView(R.id.tvItem);
                    View vLine = helper.getView(R.id.vLine);
                    tvItem.setTextColor(getResources().getColor(R.color.common_colorTextGray));
                    tvItem.setText("箱包" + position);
                    if (selectPosition == position) {
                        tvItem.setBackgroundResource(R.color.common_colorGray);
                        vLine.setVisibility(View.VISIBLE);
                    } else {
                        tvItem.setBackgroundResource(R.color.common_colorWhite);
                        vLine.setVisibility(View.INVISIBLE);
                    }
                }
            };
            mainAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    selectPosition = position;
                    mainAdapter.notifyDataSetChanged();
                }
            });
            mainRecycler.setAdapter(mainAdapter);
        }
        mainAdapter.notifyDataSetChanged();
    }

    private void initSubAdapter() {
        if (subAdapter == null) {
            subAdapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_cat_choose, subList, 20) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    TextView tvItem = helper.getView(R.id.tvItem);
                    View vLine = helper.getView(R.id.vLine);
                    tvItem.setTextColor(getResources().getColor(R.color.common_colorTextLight));
                    tvItem.setText("手机" + position);
                    tvItem.setBackgroundResource(R.color.common_colorWhite);
                    vLine.setVisibility(View.INVISIBLE);
                }
            };
            subRecycler.setAdapter(subAdapter);
        }
        subAdapter.notifyDataSetChanged();
    }
}
